<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
	        <input type="text" id="inPut" />
	        <input type="button" value="阶乘" onclick="result()" />
	        <span id="result">  </span>
	        <script type="text/javascript">
	            function result() {
	                var num = document.getElementById('inPut').value;
	                var resultSpan = document.getElementById('result');
	                var answer = 1;
					// 在js里面认为"" 等价0
					if (num == '') {
						resultSpan.innerHTML = '不能为空';
						return;
					}
					// NaN: not a number 
					// isNaN 当不是一个数字的时候返回true
	                if (isNaN(num)) {
	                    resultSpan.innerHTML = '请输入一个数字';
	                    return;
	                }
					// if (num == '' || isNaN(num)) {
					// 	resultSpan.innerHTML = '请输入一个数字';
					// 	return;
					// }
					if (num < 0) {
						 resultSpan.innerHTML = '负数没有阶乘';
						 return;
					}
					
	                if (num == 0) {
	                    resultSpan.innerHTML = '=1';
	                } else {
	                    for (var i = 1; i <= num; i++) {
	                        answer *= i;
	                    }
	                    result.innerHTML = '=' + answer;
	                }
	                
	            }
	        </script>
	    </body>
</html>
